# -*- coding:utf-8 -*-
"""
Author：Administrator
Date:2021年12月28日
"""
import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

data = pd.read_excel('./data/transformdata.xlsx')
k =4
kmodel = KMeans(n_clusters=k)
kmodel.fit(data)
r1 = pd.Series(kmodel.labels_).value_counts()
r2 = pd.DataFrame(kmodel.cluster_centers_)
r = pd.concat([r2,r1],axis=1)
r.columns=list(data.columns)+[u'聚类数量']
r3 = pd.Series(kmodel.labels_,index=data.index)
r = pd.concat([data,r3],axis=1)
r.columns=list(data.columns)+[u'聚类类别']
r.to_excel('./data/type.xlsx')
plt.rcParams['font.sans-serif']= ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
for i in range(k):
	cls = data[r[u'聚类类别'] == i]
	cls.plot(kind = 'kde',linewidth = 2,subplots=True,sharex=False)
	plt.suptitle('客户群=%d;聚类数量=%d'%(i,r1[i]))

plt.show()